package com.example.dense_rack_server.Exception;

import com.example.dense_rack_server.utils.ResponseModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 全局异常处理器
 * &#064;createTime  2020/5/8
 */
@RestControllerAdvice
//@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(AccessDeniedException.class)
    public ResponseModel handleAuthorizationException(AccessDeniedException e, HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.error(e.getMessage());
        String requestMethod = request.getMethod();
        if (requestMethod.equals("GET")) {
            response.sendRedirect("/403");
        }
        return ResponseModel.fail("没有权限，请联系管理员授权");
    }

}
